/** 龙之光辉,包括连发放龙与最短时间的雷龙放电
  游戏按键设定为:单发A,连发C(MAME按下1帧延迟5帧,游聚是连发延迟1,延迟平滑5)

  By setycyas @2024-12-24
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\RulerWindow.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作龙之光辉的宏类
*/ class DragonBlazeMacro extends CommonMacro {

  pressDuration := 30
  ;aFlag := False ;a键按下与否的标记

  /* 函数
  */
  

  /* 新增的宏函数
  */
  ;连发放龙包括了终止连发和放龙中继续连发A
  /*
  _连发放龙(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    this._down(3)
    sleep(30)
    this._up(3)
    sleep(70)
    ;放龙后如果还按住键,则连发A和连发放龙
    while (MyFuns.getVkState(vk)) {
      this._down(1)
      this._down(3)
      sleep(30)
      this._up(1)
      this._up(3)
      sleep(30)
    }
  }
  */
  ;与连发放龙键设置在一起,放龙后等一下再连发
  _延迟连发(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    if MyFuns.waitVkUp(vk, 100, 30){
      return
    } else {
      this._连发A(key)
    }
  }
  ;单发放龙,在只有连发放龙键(游聚)时用此按键保证单发
  _单发放龙(key){
    this._down(2)
    sleep(30)
    this._up(2)
    sleep(100)
  }
  ;最短时间的雷电,消耗尽量低
  _最短雷电(key){
    static DELAY := 640
    this._down(1)
    sleep(DELAY)
    this._up(1)
  }
  ;同样是最短雷电但可以按住进行持续,也就是按一下至少最短电,按住可以持续
  _最短并持续雷电(key){
    static DELAY := 640
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    this._down(1)
    sleep(DELAY)
    while(MyFuns.getVkState(vk)) {
      sleep(30)
    }
    this._up(1)
  }
  ;轻微位移用
  _右微移(key){
    MyFuns.press("right", 80)
    sleep(50)
  }
  _左微移(key){
    MyFuns.press("left", 80)
    sleep(50)
  }
  ;游聚屏幕变换
  _游聚Tab屏变换(key){  
    ;自定的两边留空和标题栏宽度
    static SIDE := 20
    static TITLE_BAR := 76
    ;窗口参数
    local wTitle, width, height
    wTitle := "(setycyas) ahk_exe x-zone.exe"
    width := 511+SIDE*2
    height := 730+TITLE_BAR
    ;执行
    MyFuns.setWindow(wTitle, width, height, 427, -38)
  }
  _游聚Tab屏还原(key){  
    ;自定的两边留空和标题栏宽度
    static SIDE := 20
    static TITLE_BAR := 76
    ;窗口参数
    local wTitle, width, height
    wTitle := "(setycyas) ahk_exe x-zone.exe"
    width := 511+SIDE*2
    height := 730+TITLE_BAR
    ;执行
    MyFuns.setWindow(wTitle, width, height, 0, 0)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    ;制作宏相关Map
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["延迟连发"] := obm(this, "_延迟连发")
    this.macroMap["单发放龙"] := obm(this, "_单发放龙")
    this.macroMap["最短雷电"] := obm(this, "_最短雷电")
    this.macroMap["最短并持续雷电"] := obm(this, "_最短并持续雷电")
    this.macroMap["左微移"] := obm(this, "_左微移")
    this.macroMap["右微移"] := obm(this, "_右微移")
    this.macroMap["游聚Tab屏变换"] := obm(this, "_游聚Tab屏变换")
    this.macroMap["游聚Tab屏还原"] := obm(this, "_游聚Tab屏还原")
    this.macroMap["空函数"] := obm(this, "_空函数")
    ;制作指定宏顺序的数组
    this.macroArr := this.macroMap.getKeys()
  }
}

/* 制作龙之光辉专用UI
*/
class DragonBlazeUI extends CommonUI {

  HOTKEY_NUM := 8
  EXE_ARRAY := [MyEmu.MAME, "arcade64.exe", MyEmu.XZONE]
  MACRO_PRIORITY := Map(
    "最短雷电", "p1", 
    "单发放龙", "p2", 
    "最短并持续雷电", "p2"
  ) 

  /* 构造函数
  */
  __New(macro){
    super.__New("龙之光辉", macro)
    this._preset := OrderMap([
      "MAME",
      ["连发A", "延迟连发", "单发放龙", "最短并持续雷电", "左微移", 
        "右微移", "空函数", "空函数"],
      "游聚",
      ["连发A", "延迟连发", "单发放龙", "最短并持续雷电", "左微移", 
        "右微移", "游聚Tab屏变换", "游聚Tab屏还原"]
    ])
    this._macro.UI := this ;绑定UI
  }

  /* 重写接口
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y230")
    this.readPresetMenu("MAME")
  }
  
  ;自定义gui组件标签页,增加一个挡板召唤键
  _customGui(){
    super._customGui()
    ;增加召唤标尺窗口按键
    this._rulerButton := this._myGui.Add("Button", "Default w200", "召唤标尺")
    this._rulerButton.OnEvent(
      "Click", 
      ObjBindMethod(this, "_onRulerButtonClick")
    )
  }

  /* 自定义函数
  */
  ;召唤标尺窗口
  _onRulerButtonClick(btnObj, info){
    static ruler := ""
    static wSize := [130, 700]
    static infos := [
      [307, "00FF00", "绿色魔法师"],
      [380, "CC00CC", "最后一关虫子"]
    ]
    static x := 70
    
    if ruler {
      ruler._gui.Restore()
    } else {
      ruler := RulerWindow(wSize, infos, x)
      ruler.show("x220 y0")
    }
  }

}

/* 运行当前脚本
*/ if (A_ScriptName == "龙之光辉.2.ahk") {
  iconFile := "..\Icon\战国之刃小夜.png"
  TraySetIcon(iconFile, , 1)

  face := FaceDetector() ;方向检测对象
  macro := DragonBlazeMacro(face, ["q","f"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := DragonBlazeUI(macro)
  ui.setup()
  ;更新组件内容
  ;ui._exeComboBox.Choose(MyEmu.XZONE)
  ui._exeComboBox.Choose(MyEmu.MAME)
  ui._gameKeyEdit.Text := "x,d"
  ui._hotkeyEdit.Text := "~s,~d,~c,~a,~e,~f,~o,~l"
  ui._fpsEdit.Text := 10
  ui._testEdit.Text := "0,20,30,30"
  ui.readPresetMenu("MAME")
}
